package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.cacheable.Cacheable;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetMealService;
import com.itheima.utils.QiniuUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * @author W.J.Y
 */
@RestController
@RequestMapping("setmeal")
public class SetMealController {

    @Reference(version = "1.0.0")
    private SetMealService setMealService;
    @Resource
    private JedisPool jedisPool;

    /**
     * 分页查询检查套餐
     * @param queryPageBean 分页查询实例
     * @return  返回结果
     */
    @RequestMapping("findPage")
    @PreAuthorize("hasAuthority('SETMEAL_QUERY')")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = setMealService.findPage(
                queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),
                queryPageBean.getQueryString());
        return pageResult;
    }

    /**
     * 检查套餐图片上传
     * @param imgFile   图片文件
     * @return  返回结果
     */
    @RequestMapping("upload")
    @PreAuthorize("hasAuthority('SETMEAL_ADD')")
    public Result upload(@RequestParam("imgFile") MultipartFile imgFile){

        try {
            //获取原文件名
            String originalFilename = imgFile.getOriginalFilename();
            //获取文件后缀
            String suffix = StringUtils.substringAfterLast(originalFilename, ".");
            //拼接文件名防止重名
            String fileName = UUID.randomUUID().toString()+"."+suffix;
            QiniuUtils.upload2Qiniu(imgFile.getBytes(),fileName);
            //保存上传成功的文件名到redis数据库
            Jedis resource = jedisPool.getResource();
            resource.sadd(RedisConstant.SETMEAL_PIC_RESOURCES,fileName);
            resource.close();
            //返回上传结果
            return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS,fileName);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false,"上传文件成功");
        }
    }

    /**
     * 新增检查套餐
     * @param setmeal 检查套餐实例
     * @return  新增操作结果
     */
    @RequestMapping("add")
    @PreAuthorize("hasAuthority('SETMEAL_ADD')")
    public Result add(@RequestBody Setmeal setmeal){
        //调用业务层添加方法
        setMealService.add(setmeal);
        //返回操作结果
        return new Result(true, "新增检查套餐成功");
    }


    /**
     * 编辑
     * @param setmeal 检查套餐实体类
     * @return  编辑操作的结果
     */
    @RequestMapping("update")
    @PreAuthorize("hasAuthority('SETMEAL_EDIT')")
    public Result update(@RequestBody Setmeal setmeal){
        //调用业务层修改方法
        setMealService.update(setmeal);
        //返回操作结果
        return new Result(true, "编辑检查套餐成功！");
    }

    /**
     * 根据ID查询检查套餐
     * @param id    检查套餐ID
     * @return  返回结果
     */
    @GetMapping("findById/{id}")
    @PreAuthorize("hasAuthority('SETMEAL_QUERY')")
    public Result findById(@PathVariable int id){
        Setmeal setmeal = setMealService.findById(id);
        return new Result(true,"查询成功",setmeal);
    }

    /**
     * 查询所有的检查套餐
     * @return
     */
    @Cacheable(expire = 600)
    @GetMapping("findAll")
    @PreAuthorize("hasAuthority('SETMEAL_QUERY')")
    public Result findById(){
        List<Setmeal> setmeals = setMealService.findAll();
        System.out.println("这里是查询所有检查套餐，我从数据库查询了！！！！");
        return new Result(true,"查询成功",setmeals);
    }

    /**
     * 删除
     * @param id 要删除的检查套餐的ID
     * @return  删除操作的结果
     */
    @GetMapping("delete/{id}")
    @PreAuthorize("hasAuthority('SETMEAL_DELETE')")
    public Result delete(@PathVariable int id){
        //调用业务层删除方法
        setMealService.delete(id);
        //返回操作结果
        return new Result(true, "删除检查套餐成功");
    }
}
